<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                <!-- 报价单号、线路号 -->
                <!-- <FormItem :label="t('xBjcost.BJNO')" type="string" v-model="baTable.form.items!.BJNO" prop="BJNO" :input-attr="{ placeholder: t('Please input field', { field: t('xBjcost.BJNO') }) }" /> -->
                <!-- <FormItem :label="t('xBjcost.LINEID')" type="number" prop="LINEID" v-model.number="baTable.form.items!.LINEID" :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('xBjcost.LINEID') }) }" /> -->
                <FormItem 
                :label="t('xBjcost.ITEMCODE')" 
                v-model="baTable.form.items!.ITEMCODE" 
                prop="ITEMCODE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=cgbj',
                    onChange:checkTimeType
                }"
                />
                <FormItem 
                :label="t('xBjcost.PRICETYPE')" 
                v-model="baTable.form.items!.PRICETYPE" 
                prop="PRICETYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=pricetype'
                }"
                />
                <FormItem 
                :label="t('xBjcost.MONEYTYPE')" 
                v-model="baTable.form.items!.MONEYTYPE" 
                prop="MONEYTYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'MONEYTYPE',
                    field: 'CURNAME',
                    'remote-url': codelib + 'curr',
                    placeholder: t('Click Select'),
                }" 
                />
                <FormItem :label="t('xBjcost.UNITPRICE')" type="number" prop="UNITPRICE" v-model.number="baTable.form.items!.UNITPRICE" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('xBjcost.UNITPRICE') }) }" />
                <FormItem 
                :label="t('xBjcost.TAXRATE')" 
                prop="TAXRATE" 
                v-model.number="baTable.form.items!.TAXRATE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=taxrate',
                    onChange:calNetPrice
                }" 
                />
                <FormItem :label="t('xBjcost.NOTAXPRICE')" type="number" prop="NOTAXPRICE" v-model.number="baTable.form.items!.NOTAXPRICE" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('xBjcost.NOTAXPRICE') }) }" />
                <!-- <FormItem :label="t('xBjcost.PRICETYPE')" type="string" v-model="baTable.form.items!.PRICETYPE" prop="PRICETYPE" :input-attr="{ placeholder: t('Please input field', { field: t('xBjcost.PRICETYPE') }) }" /> -->
                <!-- <FormItem :label="t('xBjcost.VENDOR')" type="string" v-model="baTable.form.items!.VENDOR" prop="VENDOR" :input-attr="{ placeholder: t('Please input field', { field: t('xBjcost.VENDOR') }) }" /> -->
                <!-- <FormItem :label="t('xBjcost.TRANSDAYS')" type="number" prop="TRANSDAYS" v-model.number="baTable.form.items!.TRANSDAYS" :input-attr="{ step: '0.1', placeholder: t('Please input field', { field: t('xBjcost.TRANSDAYS') }) }" /> -->
                <FormItem :label="t('xBjcost.NOTE')" type="textarea" v-model="baTable.form.items!.NOTE" prop="NOTE" :input-attr="{ placeholder: t('Please input field', { field: t('xBjcost.NOTE') }) }" />
                <FormItem :label="t('xBjcost.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: '当前登录人',disabled:true }" />
                <FormItem :label="t('xBjcost.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: '当前时间',disabled:true  }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib } from '/@/api/controllerUrls'
import createAxios from '/@/utils/axios'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    BJNO: [buildValidatorData({name: 'required', title: t('xBjcost.BJNO')})],
    ITEMCODE: [buildValidatorData({name: 'required', title: t('xBjcost.ITEMCODE')})],
    PRICETYPE: [buildValidatorData({name: 'required', title: t('xBjcost.PRICETYPE')})],
    MONEYTYPE: [buildValidatorData({name: 'required', title: t('xBjcost.MONEYTYPE')})],
    UNITPRICE: [buildValidatorData({name: 'required', title: t('xBjcost.UNITPRICE')})],
    TAXRATE: [buildValidatorData({name: 'required', title: t('xBjcost.TAXRATE')})],
    NOTAXPRICE: [buildValidatorData({name: 'required', title: t('xBjcost.NOTAXPRICE')})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xBjcost.OPTIME') })})],
})

const bjno = inject('bjno')
const lineId:any = inject('lineId')

watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.BJNO = bjno
        baTable.form.items!.LINEID = lineId.value
        baTable.form.items!.MONEYTYPE = 'RMB'
        
    }
})


const calNetPrice = ()=>{
    if(baTable.form.items!.UNITPRICE && baTable.form.items!.TAXRATE){
        baTable.form.items!.NOTAXPRICE = (baTable.form.items!.UNITPRICE / (1 + baTable.form.items!.TAXRATE)).toFixed(2)
    }
}


const checkTimeType = (val:string)=>{
    console.log(val)
    createAxios({
        url:'/admin/XBjline/freevalue?name='+val,
        method:'get'
    }).then((res:any)=>{
        console.log(res)
    })
    createAxios({
        url:'/admin/XBjline/freetime?name='+val,
        method:'get'
    }).then((res:any)=>{
        console.log(res)
    })

}


</script>

<style scoped lang="scss"></style>
